// 样式修改文件
import sgisLayerBase from '../../sfgis.layer.base';
import sgisMapBase from "../../sfgis.map.base";
import { callbackify } from 'util';

/* eslint-disable no-case-declarations */
/* *由于聚集图和等级符号图缩放时会把可见性更改，所以声明变量记录可见性 */
const layerVisible = {};
/* *初始化专题图层调用
 */
function setLayerOrder(index, layers) {
  for (const key in layers) {
    const item = layers[key];
    if (!(item instanceof Object)) {
      continue;
    }
    if(item._element !== undefined){
      item._element.style.zIndex = index;
    }
    if(item._container !== undefined){
      item._container.style.zIndex = index;
    }
  }

}
function setEchartIndex(index, layers){
  for (const key in layers) {
    const item = layers[key];
    item.echart._dom.style.zIndex = index;

  }
}
/**
 * @param {*} index number
 * @param {*} type  string
 * 调整图层顺序调用
 */
function setLayerOrder_Z(index, type, layer, layerId) {
  switch (type) {
    case "bar":
    case "line":
    case "sunburst":
    case "pyramid":
    case "radar":
    case "pie":
        setEchartIndex(index, layer.charts, layerId);
      break;
    case "cluster":
    case "clusterLayer":
    case "pock":
    case "pockingLayer":
    case "pictogram":
      layer[0]._container.style.zIndex = index;
      break;
    case "heat":
      layer.rootCanvas.style.zIndex = index;
      break;
    case "themerange":
      layer.canvas.style.zIndex = index;
      break;
    default:
      break;
  }
}
/* *查找对象所有属性最大值 */
function lookObjectMax(obj) {
  let max = 0;
  for (const key in obj) {
    const item = obj[key];
    if (item > max) {
      max = item;
    }
  }
  return max;
}
/* *对象寻找某个属性值 */
function lookObjValue(obj, value) {
  for (const key in obj) {
    const item = obj[key];
    if (item === value) {
      return {
        result: true,
        key
      };
    }
  }
  return {
    result: false
  };
}
/* *将对象的属性值整合成按照某一个数字开始连续数字 */
function formatObjValue(obj) {
  const max = lookObjectMax(obj);
  const len = Object.keys(obj).length;

  let curValue = max - 1;
  let hasValue = len - 1;
  let count = 1;

  while (hasValue) {
    const res = lookObjValue(obj, max - count);
    if (res.result) {
      obj[res.key] = curValue;
      curValue--;
      hasValue--;
    }
    count++;
  }
}

export default {
  pane: "overlayPane",
  layerOrder:{}, // 各专题图层默认顺序
  layerOrderHasGroup:{//针对基层多结果图层实现
    "clusterLayer":{},
    "pockingLayer":{},
    "heatLayer":{}
  },
  setLayerOrder, // 设置各专题图层的z-index函数，只能用于marker创建的专题图
  layerVisible, // echarts图层可见性
  curZindex: 100,//聚集图图层顺序从100递减
  //清空各专题图层默认顺序，防止重新绘制专题图失效
  clearLayerOrderHasGroup(){
    this.layerOrderHasGroup.clusterLayer = {};
    this.layerOrderHasGroup.pockingLayer = {};
    this.layerOrderHasGroup.heatLayer = {};
  },
  //分图层类型控制图层顺序
  setZIndexOffsetHasGroup(index, hashId,chartType) {
    const curIndex = this.layerOrderHasGroup[chartType][hashId];
    const layerOrderChange = {
      ...this.layerOrderHasGroup
    };
    for (const key in this.layerOrderHasGroup[chartType]) {
      const item = this.layerOrderHasGroup[chartType][key];
      if (index > 0) {//上移
        if (item <= curIndex + index && item >= curIndex) {
          if (item === curIndex) {
            layerOrderChange[chartType][key] = curIndex + index;
          } else {
            layerOrderChange[chartType][key] -= 1;
          }
        }
      } else if (index < 0) {
        if (item <= curIndex && item >= curIndex + index) {
          if (item === curIndex) {
            layerOrderChange[chartType][key] = curIndex + index;
          } else {
            layerOrderChange[chartType][key] += 1;
          }
        }
      }
    }
    this.layerOrderHasGroup[chartType] = {
      ...layerOrderChange[chartType]
    };
    formatObjValue(this.layerOrderHasGroup[chartType]);
    for (const key in this.layerOrderHasGroup[chartType]) {
      const layerInfo = sgisLayerBase.getOverLayerById(key);
      const layer = sgisMapBase.map.overlayers[layerInfo.layerId];
      if(layerInfo){
        setLayerOrder_Z(this.layerOrderHasGroup[chartType][key], layerInfo.type, layer, layerInfo.layerId);
      }
    }
  },
  setZIndexOffset(index, hashId) {
    const curIndex = this.layerOrder[hashId];
    const layerOrderChange = {
      ...this.layerOrder
    };

    for (const key in this.layerOrder) {
      const item = this.layerOrder[key];
      if (index > 0) {//上移
        if (item <= curIndex + index && item >= curIndex) {
          if (item === curIndex) {
            layerOrderChange[key] = curIndex + index;
          } else {
            layerOrderChange[key] -= 1;
          }
        }
      } else if (index < 0) {
        if (item <= curIndex && item >= curIndex + index) {
          if (item === curIndex) {
            layerOrderChange[key] = curIndex + index;
          } else {
            layerOrderChange[key] += 1;
          }
        }
      }
    }
    this.layerOrder = {
      ...layerOrderChange
    };
    formatObjValue(this.layerOrder);
    for (const key in this.layerOrder) {
      const layerInfo = sgisLayerBase.getOverLayerById(key);
      if(layerInfo){
        setLayerOrder_Z(this.layerOrder[key], layerInfo.type, layerInfo.layer, layerInfo.layerId);
      }
    }
  },

  /* *专题图层设置透明度 */
  setOpaticty(opacity) {

    for (const key in this._layers) {
      const item = this._layers[key];
      item.setOpacity(opacity);
    }
  }, // 设置各专题图层的透明度函数，只能用于marker创建的专题图
  /* *专题图层设置可见性除开等级符号图和聚集图 */
  setVisible(visible) {
    for (const key in this.charts) {
      const item = this.charts[key];
      item.echart._dom.style['visibility'] = visible;
    }
  },
  /* *专题图层设置可见性（等级符号图和聚集图） */
  setScatterVisible(visible, layerId, layer) {
    layer._container.style.visibility = visible;
    layerVisible[layerId] = visible;
  },
  //专题图层设置可见性（热力图）
  setHeatVisible(visible, layerId, layer) {
    layer.style.visibility = visible;
    layerVisible[layerId] = visible;
  },
  /* *地图缩放监听Markers绘图监听 */
  onZoom(map, layers) {
    let zIndex = '';
    map.on('zoomstart', () => {
      if (layers.length > 0) {
        try {
          zIndex = layers[0]._icon.style.zIndex;
        } catch (err) {
          return;
        }
      }
    });
    map.on('zoomend', () => {
      if (layers.length > 0) {
        try {
          layers.forEach(value => {
            value._icon.style.zIndex = zIndex;
          });
        } catch (error) {
          return;
        }
      }
    });
  },
  /* *地图缩放监听等级符号图，聚集图绘图监听 */
  onZoomScatter(map, layer, layerId) {
    map.on('zoomend', () => {
      setTimeout(() => {
        layer[0]._container.style.visibility = layerVisible[layerId];
      }, 10);
    });
    map.on('zoom', () => {
      layer[0]._container.style.visibility = layerVisible[layerId];
    });
    map.on('move', () => {
      setTimeout(() => {
        layer[0]._container.style.visibility = layerVisible[layerId];
      }, 10);
    });
    map.on('moveend', () => {
      setTimeout(() => {
        layer[0]._container.style.visibility = layerVisible[layerId];
      }, 10);
    });
  },
  onZoomHeat(map, layer, layerId) {
    map.on('zoomend', () => {
      setTimeout(() => {
        layer.rootCanvas.style.visibility = layerVisible[layerId];
      }, 10);
    });
    map.on('zoom', () => {
      layer.rootCanvas.style.visibility = layerVisible[layerId];
    });
    map.on('move', () => {
      setTimeout(() => {
        layer.rootCanvas.style.visibility = layerVisible[layerId];
      }, 10);
    });
    map.on('moveend', () => {
      setTimeout(() => {
        layer.rootCanvas.style.visibility = layerVisible[layerId];
      }, 10);
    });
  }
};
